package org.dromara.shopping.controller;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.shopping.domain.AdBanner;
import org.dromara.shopping.service.IAdBannerService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 轮播图（广告）
 *
 * @author Mr.Zhou
 * @date 2025-01-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/shopping/banner")
public class AdBannerController extends BaseController {

    private final IAdBannerService adBannerService;

    @Value("${ruoyi.imgUrl}")
    private String imgUrl;

    @Value("${ruoyi.profile}")
    private String uploadPath;

    /**
     * 查询轮播图（广告）列表
     */
    @SaCheckPermission("shopping:banner:list")
    @GetMapping("/list")
    public TableDataInfo<AdBanner> list(AdBanner adBanner, PageQuery pageQuery) {
        QueryWrapper<AdBanner> qw = new QueryWrapper<>();
        if(adBanner.getAdType() != null){
            qw.eq("ad_type", adBanner.getAdType());
        }
        qw.orderByDesc("id");

        TableDataInfo<AdBanner> td = PageUtils.buildDataInfo(adBannerService.page(PageUtils.buildPage(),qw));
        List<AdBanner> list = td.getRows();
        for (AdBanner banner : list){
            if(StrUtil.isNotEmpty(banner.getImgUrl())){
                banner.setImgUrlPath(imgUrl + banner.getImgUrl());
            }
        }
        return td;
    }

    /**
     * 导出轮播图（广告）列表
     */
    @SaCheckPermission("shopping:banner:export")
    @Log(title = "轮播图（广告）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AdBanner adBanner, HttpServletResponse response) {
        List<AdBanner> list = adBannerService.list();
        ExcelUtil.exportExcel(list, "轮播图（广告）", AdBanner.class, response);
    }

    /**
     * 获取轮播图（广告）详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("shopping:banner:query")
    @GetMapping("/{id}")
    public R<AdBanner> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer id) {
        AdBanner adBanner = adBannerService.getById(id);
        if(adBanner != null){
            if(StrUtil.isNotEmpty(adBanner.getImgUrl())){
                adBanner.setImgUrlPath(imgUrl + adBanner.getImgUrl());
            }
        }
        return R.ok(adBanner);
    }

    /**
     * 新增轮播图（广告）
     */
    @SaCheckPermission("shopping:banner:add")
    @Log(title = "轮播图（广告）", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AdBanner adBanner) {
        return toAjax(adBannerService.save(adBanner));
    }

    /**
     * 修改轮播图（广告）
     */
    @SaCheckPermission("shopping:banner:edit")
    @Log(title = "轮播图（广告）", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AdBanner adBanner) {
        return toAjax(adBannerService.updateById(adBanner));
    }

    /**
     * 删除轮播图（广告）
     *
     * @param ids 主键串
     */
    @SaCheckPermission("shopping:banner:remove")
    @Log(title = "轮播图（广告）", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Integer[] ids) {
        return toAjax(adBannerService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
